/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bt.dmdb.service.impl;

/**
 *
 * @author manjunathad
 */
import com.bt.dmdb.dao.LoginUsersDao;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.fatca.dao.UserDAO;
import javax.annotation.Resource;

@Service("userDetailsService")
@Transactional(value = "txmDmdb")
public class CustomUserDetailsService implements UserDetailsService {

//    @Autowired
//    private UserDAO userDAO;

    @Resource
    private LoginUsersDao usersDao;

    public LoginUsersDao getUsersDao() {
        return usersDao;
    }

    public void setUsersDao(LoginUsersDao usersDao) {
        this.usersDao = usersDao;
    }

    
    @Transactional(value = "txmDmdb", readOnly = true)
    public UserDetails loadUserByUsername(String login)
            throws UsernameNotFoundException {

        com.bt.dmdb.model.LoginUser domainUser = usersDao.getUser(login);

        boolean enabled = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean accountNonLocked = true;

        return new User(
                domainUser.getFirstname(),
                domainUser.getPassword(),
                enabled,
                accountNonExpired,
                credentialsNonExpired,
                accountNonLocked,
                getAuthorities(domainUser.getRole().getId().intValue()));
    }

    public Collection<? extends GrantedAuthority> getAuthorities(Integer role) {
        List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));
        return authList;
    }

    public List<String> getRoles(Integer role) {

        List<String> roles = new ArrayList<String>();

        if (role.intValue() == 1) {
//			roles.add("ROLE_MODERATOR");
            roles.add("ROLE_ADMIN");
        } else if (role.intValue() == 2) {
            roles.add("ROLE_MODERATOR");
        }
        return roles;
    }

    public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) {
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

        for (String role : roles) {
            authorities.add(new SimpleGrantedAuthority(role));
        }
        return authorities;
    }
}
